//
//  EncapsulationMethod.m
//  小胖兔旅行指南
//
//  Created by KANGEL on 15/4/13.
//  Copyright (c) 2015年 小胖兔. All rights reserved.
//

#import "EncapsulationMethod.h"
#import "Reachability.h"
#import "EncapsulationTools.h"


@implementation EncapsulationMethod

- (void)dealloc
{
    [_receiveData release];
    [_requestUrl release];
    Block_release(_finishBlock);
    [super dealloc];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.receiveData = [NSMutableData data];
        
    }
    return self;
}

//判断网络连接种类
+ (BOOL)netWorkReachability
{
    //进入一个可用网址，用来判断网络是否连接
    Reachability *reach = [Reachability reachabilityWithHostName:@"http://www.baidu.com"];
    //判断通过什么方式连接的
    NetworkStatus status = [reach currentReachabilityStatus];
    
    switch (status) {
        case NotReachable:
//            NSLog(@"no net");
            return NO;
            break;
        case ReachableViaWiFi:
//            NSLog(@"wifi");
            return YES;
            break;
        case ReachableViaWWAN:
//            NSLog(@"3g");
            return YES;
            break;
            
        default:
            break;
    }
}

+ (void)connectWithUrl:(NSString *)urlStr parmaters:(NSDictionary *)parmaters finishBlock:(void(^)(NSData * data))block
{
    //判断是否联网 是 则网络请求 否 读取本地缓存
    BOOL judge = [EncapsulationMethod netWorkReachability];
    if (judge) {//是
        
        EncapsulationMethod *connect = [[EncapsulationMethod alloc] init];
        
        connect.finishBlock = block;
        [connect startConnectWithUrl:urlStr parmaters:parmaters];
        [connect release];
    }else {//否
        
        NSString *url = [EncapsulationMethod urlWithName:urlStr parmaters:parmaters];
        
        NSData *data = [EncapsulationTools readWithName:url directory:NSLibraryDirectory];
        
        block(data);
    }
}



//网址拼接
+ (NSString *)urlWithName:(NSString *)urlStr parmaters:(NSDictionary *)parmaters
{
    NSString *par = @"";
    for (NSString *key in [parmaters allKeys]) {
        if ([par length] == 0) {
            par = [NSString stringWithFormat:@"?%@=%@", key, [parmaters objectForKey:key]];
        }else {
            par = [NSString stringWithFormat:@"%@&%@=%@", par, key, [parmaters objectForKey:key]];
        } 
    }
    urlStr = [NSString stringWithFormat:@"%@%@", urlStr, par];
    return urlStr;
}

//网络请求

- (void)startConnectWithUrl:(NSString *)urlstr parmaters:(NSDictionary *)parmaters
{
    //拼接url
    urlstr = [EncapsulationMethod urlWithName:urlstr parmaters:parmaters];
    //将空格和汉子全部转换为UTF8
    urlstr = [urlstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    self.requestUrl = urlstr;
    
    //网络请求
    NSURL *url = [NSURL URLWithString:urlstr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    
    [NSURLConnection connectionWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receiveData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.finishBlock(self.receiveData);
    
    [EncapsulationTools writeWithName:self.requestUrl data:self.receiveData directory:NSLibraryDirectory];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"网络请求错误=======%@", error);
}

@end
